<?php

namespace app\api\controller\feiyu;

use app\common\controller\Api;
use app\common\exception\UploadException;
use think\Config;
use think\Hook;


/**
 * 公共接口
 */
class Base extends Api {

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';
    
    
    public $appid = '1809415148357852';
    public $secret = '4caf904f36cf34e2c2e18bf2b4edb0e5e6072dda';
    
    
    
    


    public $access_token = '';
    public $advertiser_ids = [];

    public function _initialize() {

        if (isset($_SERVER['HTTP_ORIGIN'])) {
            header('Access-Control-Expose-Headers: __token__'); //跨域让客户端获取到
        }
        //跨域检测
        check_cors_request();
        $info = $this->getFeiyuToken(); 
        parent::_initialize();
    }
   



    public function getFeiyuToken() {
        $info = db('ec_token')->where('platform','feiyu')->find();
        if($info && $info['access_token'] && $info['expire'] > time()){
//            $config = json_decode($info['config'],true);
//            $token_info = $config['data'];
//            $this->advertiser_ids = empty($token_info['advertiser_ids']) ? [] : $token_info['advertiser_ids'];
            $this->access_token = $info['access_token'];
            return ['status'=>2000,'token'=>$info['access_token']];
        }
        
        if($info && $info['access_token']){
            return $this->refreshToken();
        }
        
        $data = ['app_id'=>$this->appid,'secret'=>$this->secret,'auth_code'=>$info['auth_code'],'grant_type'=>'auth_code'];
        
        $url = 'https://ad.oceanengine.com/open_api/oauth2/access_token/';
        $response = httpCurlpost($url, json_encode($data));
        $result = json_decode($response,true);
        qqq('feiyu',$result);
        $grant_type = 'platform';
        if(empty($result['code']) && $result['message'] == 'OK'){
            db('ec_token')->where('platform','feiyu')->update([
                'access_token'=> $result['data']['access_token'],
                'expire'=> $result['data']['expires_in'] + time(),
                'auth_code'=>'',
                'config'=> json_encode($result,JSON_UNESCAPED_UNICODE),
                'updatetime'=>time()
            ]);
//            $this->advertiser_ids = $result['data']['advertiser_ids'];
            $this->access_token = $result['data']['access_token'];
            
            return ['status'=>2000,'token'=>$result['data']['access_token']];
        }
        
        return ['status'=>4000,'msg'=>'获取失败'];

    }
    
    
    public function refreshToken() {
        $info = db('ec_token')->where('platform','feiyu')->find();
        
        $config = json_decode($info['config'],true);
        $token_info = $config['data'];
        
        $data = ['app_id'=>$this->appid,'secret'=>$this->secret,'refresh_token'=>$token_info['refresh_token']];
        
        $url = 'https://api.oceanengine.com/open_api/oauth2/refresh_token/';
        $headers=["Access-Token: " . $token_info['access_token']];
        $response = httpCurlpost($url, json_encode($data),$headers);
        $result = json_decode($response,true);
        qqq('refreshToken',$result);
        $grant_type = 'platform';
        if(empty($result['code']) && $result['message'] == 'OK'){
            db('ec_token')->where('platform','feiyu')->update([
                'access_token'=> $result['data']['access_token'],
                'expire'=> $result['data']['expires_in'] + time(),
                'config'=> json_encode($result,JSON_UNESCAPED_UNICODE),
                'updatetime'=>time()
            ]);
//            $this->advertiser_ids = $result['data']['advertiser_ids'];
            $this->access_token = $result['data']['access_token'];
            return ['status'=>2000,'token'=>$result['data']['access_token']];
        }
        
        return ['status'=>4000,'msg'=>'获取失败[刷新]'];

    }
    
    
    

    

}
